package com.apes.fn.server.productRequire;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.event.Listener;
import com.apes.framework.plugin.outbox.OutboxPayloadWrapper;
import com.apes.framework.plugin.outbox.OutboxSubscriberService;
import com.apes.framework.plugin.outbox.annotation.OutboxSubscriber;
import com.apes.pi.service.PiManager;
import com.apes.scm.pur.purchaseOrder.model.PurchaseOrder;
import com.apes.scm.pur.purchaseOrder.repository.PurchaseOrderRepository;
import com.apes.scm.stock.transfer.model.Pick;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service("requireChangeQtyService")
public class RequireChangeQtyService {
    @Autowired
    private PiManager piManager;
    @Autowired
    private OutboxSubscriberService outboxSubscriberService;
    @Autowired
    private PurchaseOrderRepository purchaseOrderRepository;

    @Value("${apes.switched:false}")
    private Boolean switched;

    /**
     * 销售出库收货回写老系统实际到货数量
     * */
//    @Listener(topic = "event:stock.pick.delivery")
    public void appendRequestData(SimpleRequest request) {
        if (switched) return;
        JSONObject getJO = request.getJO();
        if (!"PurchaseOrder".equals(getJO.getString("originBusinessType"))) return;
        PurchaseOrder purchaseOrder = purchaseOrderRepository.findOne(getJO.getString("origin"));
        if (!"ZD2".equals(purchaseOrder.getSupplierType())) return;
        JSONObject param = new JSONObject();
        param.put("ZCDH", purchaseOrder.getSupplierRef());
        outboxSubscriberService.commonSendRequest("local:RequireChangeQtyService.updateOldYHDRequest", "updateYHDSJDHSL: " + getJO.getString("pickId"), param);
    }

    @OutboxSubscriber(id="local:RequireChangeQtyService.updateOldYHDRequest", name="修改老系统要货实际到货数量")
    private void updateOldYHDRequest(OutboxPayloadWrapper outboxPayloadWrapper) {
        piManager.synErp("scmYhdChangeQtyService.updateDC_YHSLGZ_SH", outboxPayloadWrapper.getPayload());
    }
}
